/* All functions defined here are used by multiple html pages */

	/* Checks if the user is logged in, and if not redirects to login page 
	* Returns true if user is logged in, false if not logged in
	*/
	function checkLogin() {
		var url = 'lib/userApi.php?__m=isLoggedIn&__d=';
		
		var para = {};
		
		//encode the parameters
		var para_encoded = JSON.stringify(para);
		
		url += para_encoded;
		var retval;
		
	    $.ajax({
		  url: url,
		  async:false,
		  success: function( result ) {
		   		var msg = jQuery.parseJSON(result);
		   		retval = msg.success;
		  },
		  error: function(){
		  		alert('oops, we got an error!');
		  }
		});

		return retval;	
	}
	function getParameterByName( name )
	{
	    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	    var regexS = "[\\?&]"+name+"=([^&#]*)";
	    var regex = new RegExp( regexS );
	    var results = regex.exec( window.location.href );
	    if( results == null )
	      return "";
	    else
	      return decodeURIComponent(results[1].replace(/\+/g, " "));
	}

	
	function bindButtons() {
		//Grab project id and release id from project url
		var project_id = getParameterByName( '__pid' );
		var release_id = getParameterByName( '__rid' );
		//alert( 'pid = ' + project_id + ' rid = ' + release_id);
		
		var querystring = '';
		if( project_id != '' && release_id != '')
			querystring = '?__pid=' + project_id + '&__rid=' + release_id;
		
		//Listeners for the buttons
		$(".menu-btn .logout").click(function() {
			handleLogout();
		});
		$(".menu-btn .workspace").click(function() {
			window.location ='/workspace.html'+querystring;
		});
		$(".menu-btn .account").click(function() {
			window.location ='/account-settings.html'+querystring;
		});		
		$(".menu-btn .project-revision-sel").click(function() {
			window.location ='/project-rev-switch.html'+querystring;
		});

	}
	
	/* A handler that logs out and redirects to the login page */
	function handleLogout() {
		var url = 'lib/userApi.php?__m=logout&__d=';
		
		var para = {};
			
		//encode the parameters
		var para_encoded = JSON.stringify(para);
			
		url += para_encoded;
	
		//ajax call to get result
		$.ajax({
		  url: url,
		  async:false,
		  success: function( result ) {
		  		//No return values from logout
		   		var msg = jQuery.parseJSON(result);
		   		window.location = '/index.html';
		  },
		  error: function(){
		  		alert('oops, we got an error!');
		  }
		});
	}
	// Call a function api in a file
	// Calls callback on success or errback on failure
	// Callback and errback are passed the returned object and self
	
	var callApi = function (file, api, type, obj, callback, errback, self, async) {
	
	if (typeof async === 'undefined') { async = true };
	if (typeof type !== 'string') { alert("Must include GET or POST as AJAX parameter.") }
	if (typeof file !== 'string') { alert("Include file name string in api call."); }
	if (typeof api !== 'string') { alert("Include api name string in api call."); }

	// Construct the json string
	var url = 'lib/' + file + '.php';
	var jsonParameters = JSON.stringify(obj);
	
	// Call database,
	$.ajax({
		type: type,
		url: url,
		async: async,
		data: {__m: api, __d: jsonParameters},
		success: function(resultString) {
			var resultObj = jQuery.parseJSON(resultString);
			
			// Call the callback function on success
			if (callback && typeof(callback) === 'function') {
				callback(resultObj, self);
			}
		},
		error: function(){
			
			// Call the errback function on failure
			if (errback && typeof(errback) === 'function') {
				errback(self);
			} else {
				alert('Unable to connect to database.')
			}
		}
	});
}